//
//  DLInfoTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/26.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLInfoTC.h"
#import "DLInfoListModel.h"


@interface DLInfoTC ()


@property (nonatomic, strong) UILabel *authorLabel;

@property (nonatomic, strong) UILabel *addTimeLabel;

@property (nonatomic, strong) UILabel *viewCountLabel;

@property (nonatomic, strong) UIImageView *introImageView;

@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation DLInfoTC


- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = kColorRandom;
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutsubControls];
    }
    return self;
}

- (void) layoutsubControls {
    
    CGFloat imageWidth = 90;
    CGFloat imageHeight = 68;
    CGFloat margin = 15;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(margin, 30, kScreenWidth - margin * 2 - 10 - imageWidth, 15);
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.font = kSystemFont(15);
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment =NSTextAlignmentLeft;
    titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
//    titleLabel.backgroundColor = [UIColor redColor];
    
    
    UILabel *authorLabel = [UILabel new];
    authorLabel.frame = CGRectMake(titleLabel.left, 120 - 14 - 20, titleLabel.width *0.45, 14);
    authorLabel.textColor = kColorRGB(168, 168, 168);
    authorLabel.font = kSystemFont(14);
    authorLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:authorLabel];
    self.authorLabel = authorLabel;
//    authorLabel.backgroundColor = [UIColor orangeColor];
    
    UILabel *addTimeLabel = [UILabel new];
    addTimeLabel.frame = CGRectMake(authorLabel.right, authorLabel.top, titleLabel.width *0.35, authorLabel.height);
    addTimeLabel.textColor = kColorRGB(168, 168, 168);
    addTimeLabel.font = kSystemFont(14);
    addTimeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:addTimeLabel];
    self.addTimeLabel = addTimeLabel;
//    addTimeLabel.backgroundColor = [UIColor yellowColor];
    
    UILabel *viewCountLabel = [UILabel new];
    viewCountLabel.frame = CGRectMake(addTimeLabel.right, addTimeLabel.top, titleLabel.width *0.2, addTimeLabel.height);
    viewCountLabel.textColor = kColorRGB(168, 168, 168);
    viewCountLabel.font = kSystemFont(14);
    viewCountLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:viewCountLabel];
    self.viewCountLabel = viewCountLabel;
//    viewCountLabel.backgroundColor = [UIColor greenColor];
    
    UIImageView *introImageView = [UIImageView new];
    introImageView.frame = CGRectMake(kScreenWidth - margin - imageWidth, (120 - imageHeight) / 2, imageWidth, imageHeight);
    introImageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:introImageView];
    self.introImageView = introImageView;
    
    UILabel *bottomLabel = [UILabel new];
    bottomLabel.frame = CGRectMake(0, 119, kScreenWidth, 1);
    bottomLabel.backgroundColor = kColorRGB(240, 240, 240);
    [self.contentView addSubview:bottomLabel];
    self.bottomLabel = bottomLabel;
}

- (void) setInfoModel:(DLInfoListModel *)infoModel {
    
    _infoModel = infoModel;
    NSString *title = [NSString stringWithFormat:@"%@", infoModel.title];
    NSString *author = [NSString stringWithFormat:@"%@", infoModel.source];
    NSString *addTime = [NSString stringWithFormat:@"%@", infoModel.pubdate_2];
    
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter  setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *updateDate = [dateFormatter dateFromString:addTime];
//
//    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
//    [dateFormatter1 setDateFormat:@"MM-dd HH:mm"];
//    NSString *addTimeString = [dateFormatter1 stringFromDate:updateDate];
//
//    NSString *viewCount = infoModel.view_count;
    
    NSString *base64String = [NSString stringWithFormat:@"%@", infoModel.head_img];

//    NSArray *arr11111 = [base64String componentsSeparatedByString:@","];
//
//    NSData *data = [[NSData alloc]initWithBase64EncodedString:[arr11111 lastObject] options:NSDataBase64DecodingIgnoreUnknownCharacters];
//
//    NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"--------------%@", string);

    
    if ([base64String containsString:@"data:image"]) {
        self.introImageView.hidden = NO;
        NSArray *arr = [base64String componentsSeparatedByString:@","];
        // 将base64字符串转为NSData
        NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:[arr lastObject] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
        // 将NSData转为UIImage
        UIImage *decodeImage = [UIImage imageWithData:decodeData];
        [self.introImageView setImage:decodeImage];
        
        //90 - 30 - 10
        [self.titleLabel resetWidth:kScreenWidth - 130];
        
        CGFloat newTitleSizeHeight = [title getValue4HeightWithWid:kScreenWidth - 130 andFont:kSystemFont(15)];
        [self.titleLabel resetHeight:newTitleSizeHeight];
    } else {
        self.introImageView.hidden = YES;
        [self.titleLabel resetWidth:kScreenWidth - 30];
        CGFloat newTitleSizeHeight = [title getValue4HeightWithWid:kScreenWidth - 30 andFont:kSystemFont(15)];
        [self.titleLabel resetHeight:newTitleSizeHeight];
    }
    
    
    self.titleLabel.text = title;
    self.authorLabel.text = author;
    self.addTimeLabel.text = addTime;
    self.viewCountLabel.text = @"";
    
    if (infoModel.isNewsRead == YES) {
        self.titleLabel.textColor = kColorRGB(119, 119, 119);
    } else {
        self.titleLabel.textColor = kDLNormalBlackColor;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
